package com.privacy.accountmgr.utils

import android.app.Application
import android.content.Context

object GlobalContext {
    private var mContext: Application? = null
    /**
     * 通过反射获取全局Context
     * @return
     */
    val context: Context?
        get() {
            if (mContext == null) {
                var app: Application? = null
                try {
                    app = Class.forName("android.app.AppGlobals").getMethod("getInitialApplication").invoke(null) as Application
                    if (app == null)
                        throw IllegalStateException("Static initialization of Applications must be on main thread.")
                } catch (e: Exception) {
                    try {
                        app = Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null) as Application
                    } catch (ex: Exception) {
                        ex.printStackTrace()
                    }
                } finally {
                    mContext = app
                }
            }
            return mContext
        }

}
